﻿using AuthCenter.ErorCode;
using AuthCenter_API.Read.Applications.Commands.RBAC;
using AuthCenter_API.Write.Applications.Commands.Audit;
using AuthCenter_API.Write.Applications.Commands.RBAC;
using AuthCenter_API.Write.Applications.Commands.User;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace AuthCenter_API.Write.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class ManagementController : ControllerBase
    {
        private readonly ILogger<ManagementController> logger;
        private readonly IMediator mediator;

        public ManagementController(ILogger<ManagementController> logger, IMediator mediator)
        {
            this.logger = logger;
            this.mediator = mediator;
        }
        /// <summary>
        /// 新增用户
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<APIResult<object>> AddUser(AddUserCommand command)
        {
            try
            {
                logger.LogInformation("新增用户成功");
                return await mediator.Send(command);
            }
            catch (Exception ex)
            {
                logger.LogError("新增用户失败"+ex.Message);
                throw;
            }
        }

        /// <summary>
        /// 修改用户
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<APIResult<object>> UpdUser(UpdUserCommand command)
        {
            try
            {
                logger.LogInformation("修改用户成功");
                return await mediator.Send(command);
            }
            catch (Exception ex)
            {
                logger.LogError("修改用户失败" + ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<APIResult<object>> DelUser(DelUserCommand command)
        {
            try
            {
                logger.LogInformation("删除用户成功");
                return await mediator.Send(command);
            }
            catch (Exception ex)
            {
                logger.LogError("删除用户失败" + ex.Message);
                throw;
            }
        }


        /// <summary>
        /// 新增角色
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<APIResult<object>> AddRole(RoleAddCommand command)
        {
            try
            {
                logger.LogInformation("新增角色成功");
                return await mediator.Send(command);
            }
            catch (Exception ex)
            {
                logger.LogError("新增角色失败" + ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 修改角色
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<APIResult<object>> UpdRole(RoleUpdCommand command)
        {
            try
            {
                logger.LogInformation("修改角色成功");
                return await mediator.Send(command);
            }
            catch (Exception ex)
            {
                logger.LogError("修改角色失败" + ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 删除角色
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<APIResult<object>> DelRole(RoleDelCommand command)
        {
            try
            {
                logger.LogInformation("删除角色角色户成功");
                return await mediator.Send(command);
            }
            catch (Exception ex)
            {
                logger.LogError("删除用户失败" + ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 角色分配保存
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<APIResult<object>> AddDep(FenpeiAddCommand command)
        {
              return await mediator.Send(command);
        }
        /// <summary>
        /// 删除用户名称
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<APIResult<object>> DelUserName(DelRoleDepCommand command)
        {
            return await mediator.Send(command);
        }
        /// <summary>
        /// 新增菜单
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<APIResult<object>> AddMenu(MenuAddCommand command)
        {
            return await mediator.Send(command);
        }
        /// <summary>
        /// 删除菜单
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<APIResult<object>> DelMenu(MenuDelCommand command)
        {
            return await mediator.Send(command);
        }
        /// <summary>
        /// 修改菜单
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<APIResult<object>> UpdMenu(MenuUpdCommnad command)
        {
            return await mediator.Send(command);
        }
        /// <summary>
        /// 添加角色菜单
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<APIResult<object>> AddRoleMenu(RoleMenuAddCommand command)
        {
            return await mediator.Send(command);
        }
        /// <summary>
        /// 删除审批人
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<APIResult<object>> DelAudit(AuditDelCommand command)
        {
            return await mediator.Send(command);
        }
        /// <summary>
        /// 新增审批人
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<APIResult<object>> AddAudit(AuditAddCommand command)
        {
            return await mediator.Send(command);
        }
        /// <summary>
        /// 修改审批人
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<APIResult<object>> UpdAudit(AuditUpdCommand command)
        {
            return await mediator.Send(command);
        }
        /// <summary>
        /// 审批
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<APIResult<object>> UpdAuditState(AuditUpdStateCommand command)
        {
            return await mediator.Send(command);
        }
    }
}
